//
//  MyViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/8.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//
#import "RDVTabBarController.h"
#import "MyViewController.h"
#import "TheLoginViewController.h"
#import "SystemSettingsViewController.h"
#import "ToBindCompanyViewController.h"
#import "MJRefresh.h"
#import "MembershipDetailsViewController.h"
#import "MemberCardRecordsViewController.h"
#import "UMSocial.h"
#import "TradesViewController.h"
#import "MemberActivityViewController.h"
#import "MCouponViewController.h"
#import "MemberInformationViewController.h"
#import "MemberfindIntegralListViewController.h"
#import "MessageViewController.h"
#import "UIImageView+WebCache.h"
@interface MembershipDetailsViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate, UITextViewDelegate,UIImagePickerControllerDelegate,UIAlertViewDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate,UMSocialUIDelegate,MemberInformationViewControllerDelegate>

{
    MJRefreshHeaderView* _header;
    MJRefreshFooterView* _footer;
    NSArray *array;
    NSArray *arrayImage;
    NSString* tradeMoney;
    NSString* couponQty;
    NSString* actQty;
    NSString* integral;
    NSString* cardMoney;
    UIButton*name;
    UIImageView *imageView;
    UIView *headerView;
    UIButton* theArrowBtn;
    NSDictionary *DataLine;
    UIButton *TheMessageBtn;
    NSString* userStatus;
    UITableView*table;
    NSDictionary*Dict;
}
@end

@implementation MembershipDetailsViewController
@synthesize alt = alt;
-(void)BackAction:(UIButton *)sender{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.view.backgroundColor=[UIColor whiteColor];
    [self setNavi];
}
-(void)xiangqing:(UIButton*)sender{
    
    MemberInformationViewController *detailViewController = [[MemberInformationViewController alloc] init];
    detailViewController.memberId=self.memberId;
    detailViewController.delegate=self;
    [self.navigationController pushViewController:detailViewController animated:YES];
    
}
-(void)update:(NSString *)Str{
 [self setNavi];
 [self update];

}


-(void)viewDidLoad{
    
    [super viewDidLoad];
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    
    [self update];
    
}
- (void)update
{
    
    [headerView removeFromSuperview];
    headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,110)];
    table.tableHeaderView = headerView;
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:self.memberId forKey:@"memberId"];
    //    NSLog(@"====%@",param);[Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    
    [HttpRequest requestData:@"Business/Member/Detail" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"33333333====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                tradeMoney=[severDataDict.dataDict objectForKey:@"tradeMoney"];
                couponQty=[severDataDict.dataDict objectForKey:@"couponQty"];
                actQty=[severDataDict.dataDict objectForKey:@"actQty"];
                integral=[severDataDict.dataDict objectForKey:@"integral"];
                cardMoney=[severDataDict.dataDict objectForKey:@"cardMoney"];
                Dict=severDataDict.dataDict;
               [self table];
           
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:requestResultStr delegate:self cancelButtonTitle:@"确定返回" otherButtonTitles: nil];
                [alert show];
                
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];
    
}

- (void)table
{
    
    
    arrayImage = @[@"huiyuan-jiaoyimingxi",@"huiyuan-youhuiquan",@"huiyuancuxiaohuodong",@"huiyuancuxiaohuodong",@"huiyuanchuzhijilu"];
    array=@[@"交易明细",@"优惠券",@"促销活动",@"积分明细",@"储值记录"];

    
    
   
    UIImageView* heardimage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 100)];
    heardimage.image=[UIImage imageNamed:@""];
    [headerView addSubview:heardimage];
    UIButton*Btn=[[UIButton alloc]init];
    Btn.backgroundColor=[UIColor clearColor];
    Btn.frame=CGRectMake(0, 0, ScreenWidth, 100);
    [Btn addTarget:self action:@selector(xiangqing:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:Btn];
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(16.5, 18.5, 62.5, 62.5)];
    imageView.layer.masksToBounds = YES;
    imageView.layer.cornerRadius = 62.5/2;
    imageView.contentMode = UIViewContentModeScaleToFill;
    [imageView setImageWithURL:[NSURL URLWithString:[Dict objectForKey:@"headImage"]] placeholderImage:[UIImage imageNamed:@"touxiang_weidenglu"]];
    
    imageView.layer.borderColor = qianhuiColor.CGColor;
    imageView.layer.borderWidth = 0;
    [headerView addSubview:imageView];
    UIImageView* heardimage1=[[UIImageView alloc]initWithFrame:CGRectMake(15, 17.5, 65, 65)];
    heardimage1.image=[UIImage imageNamed:@"logoyuanhuansss"];
    [headerView addSubview:heardimage1];
    UIView*huiseview=[[UIView alloc]initWithFrame:CGRectMake(0,100, ScreenWidth, 10)];
    huiseview.backgroundColor=zuiqianhuiColor;
    huiseview.layer.borderWidth = 0.35;
    huiseview.layer.borderColor = qianhuiColor.CGColor;
    [headerView addSubview:huiseview];
    NSString *s2 =[NSString stringWithFormat:@"%@(%@)",  [Dict objectForKey:@"userName"], [Dict objectForKey:@"phone"]];
    UIFont *font2 = [UIFont systemFontOfSize:16];
    CGSize size2 = CGSizeMake(ScreenWidth-60, 2000);
    CGSize labelsize2 = [s2 sizeWithFont:font2 constrainedToSize:size2 lineBreakMode:NSLineBreakByCharWrapping];
    
    theArrowBtn=[[UIButton alloc]init];
    theArrowBtn.backgroundColor=[UIColor clearColor];
    theArrowBtn.frame=CGRectMake(ScreenWidth-30, 45, 8, 13);
    [theArrowBtn setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
    [theArrowBtn addTarget:self action:@selector(xiangqing:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:theArrowBtn];
    name=[[UIButton alloc]initWithFrame:CGRectMake(imageView.right+10, 64/2, labelsize2.width, 16)];
    [name setTitle:s2 forState:UIControlStateNormal];
    name.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
    [name setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    name.titleLabel.font=[UIFont systemFontOfSize:16];
    [headerView addSubview:name];
    
    UIImageView*imageViews=[[UIImageView alloc]initWithFrame:CGRectMake(name.right, 60/2, 20, 20)];
    [headerView addSubview:imageViews];
    if (![[Dict objectForKey:@"sex"] isEqual:[NSNull null]]) {
        
        if ([[Dict objectForKey:@"sex"]isEqualToString:@"男"]) {
            
            
            imageViews.image=[UIImage imageNamed:@"nan"];
            
        }else{
            
            imageViews.image=[UIImage imageNamed:@"nv"];
            
        }
    }else{
        imageViews.image=[UIImage imageNamed:@"nv"];
        
    }
    
    UILabel*level=[[UILabel alloc]initWithFrame:CGRectMake(imageView.right+10, name.bottom+8, 30, 13)];
    level.font=[UIFont systemFontOfSize:13];
    level.text=@"等级";
    level.textColor=zhonghuiColor;
    [headerView addSubview:level];
  

     [table reloadData];
}


#pragma mark--TableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.font=[UIFont systemFontOfSize:15];
    
    cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[arrayImage objectAtIndex:indexPath.row]]];
    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    cell.textLabel.textColor=[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1];
    
    if ([[array objectAtIndex:indexPath.row] isEqualToString:@"交易明细"]) {
        
        
        NSString*GoldPrice1=[NSString stringWithFormat:@"%@",tradeMoney];
        NSNumberFormatter *numberFormatter1 = [[NSNumberFormatter alloc] init];
        [numberFormatter1 setPositiveFormat:@"###,##0.00;"];
        NSString *formattedNumberString1 = [numberFormatter1 stringFromNumber:[NSNumber numberWithDouble:GoldPrice1.floatValue]];
        UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-205, 0, 160, 50)];
        label.text=[NSString stringWithFormat:@"¥ %@",formattedNumberString1];
        label.textAlignment=NSTextAlignmentRight;
        label.textColor=zhonghuiColor;
        label.font=[UIFont systemFontOfSize:  contenfot];
        [cell.contentView addSubview:label];
        UIButton*jiantou = [[UIButton alloc]init];
        jiantou.frame = CGRectMake(ScreenWidth-30,18, 8, 13);
        [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
        [cell.contentView addSubview:jiantou];    }
    if ([[array objectAtIndex:indexPath.row] isEqualToString:@"优惠券"]) {
        UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-205, 0, 160, 50)];
        label.text=[NSString stringWithFormat:@"%@",couponQty];
        label.textAlignment=NSTextAlignmentRight;
        label.textColor=zhonghuiColor;
        label.font=[UIFont systemFontOfSize:  contenfot];
        [cell.contentView addSubview:label];
        UIButton*jiantou = [[UIButton alloc]init];
        jiantou.frame = CGRectMake(ScreenWidth-30,18, 8, 13);
        [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
        [cell.contentView addSubview:jiantou];
    }
    if ([[array objectAtIndex:indexPath.row] isEqualToString:@"促销活动"]) {
        UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-205, 0, 160, 50)];
        label.text=[NSString stringWithFormat:@"%@",actQty];
        label.textAlignment=NSTextAlignmentRight;
        label.textColor=zhonghuiColor;
        label.font=[UIFont systemFontOfSize:  contenfot];
        [cell.contentView addSubview:label];
        UIButton*jiantou = [[UIButton alloc]init];
        jiantou.frame = CGRectMake(ScreenWidth-30,18, 8, 13);
        [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
        [cell.contentView addSubview:jiantou];
    }
    if ([[array objectAtIndex:indexPath.row] isEqualToString:@"积分明细"]) {
        
        NSString*GoldPrice1=[NSString stringWithFormat:@"%@",integral];
        NSNumberFormatter *numberFormatter1 = [[NSNumberFormatter alloc] init];
        [numberFormatter1 setPositiveFormat:@"###,##0.00;"];
        NSString *formattedNumberString1 = [numberFormatter1 stringFromNumber:[NSNumber numberWithDouble:GoldPrice1.floatValue]];
        UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-205, 0, 160, 50)];
        label.text=[NSString stringWithFormat:@"¥ %@",formattedNumberString1];
        label.textAlignment=NSTextAlignmentRight;
        label.textColor=zhonghuiColor;
        label.font=[UIFont systemFontOfSize:  contenfot];
        [cell.contentView addSubview:label];
        UIButton*jiantou = [[UIButton alloc]init];
        jiantou.frame = CGRectMake(ScreenWidth-30,18, 8, 13);
        [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
        [cell.contentView addSubview:jiantou];
    }
    if ([[array objectAtIndex:indexPath.row]isEqualToString:@"储值记录"]) {
        UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-205, 0, 160, 50)];
        label.text=[NSString stringWithFormat:@"%@",cardMoney];
        label.textAlignment=NSTextAlignmentRight;
        label.textColor=zhonghuiColor;
        label.font=[UIFont systemFontOfSize:  contenfot];
        [cell.contentView addSubview:label];
        UIButton*jiantou = [[UIButton alloc]init];
        jiantou.frame = CGRectMake(ScreenWidth-30,18, 8, 13);
        [jiantou setImage:[UIImage imageNamed:@"xiangzuo-.png"] forState:UIControlStateNormal];
        [cell.contentView addSubview:jiantou];    }
    

    
    if (array.count !=indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(60, 50-0.35, ScreenWidth-75, 0.35)];
        lineLabel1.backgroundColor=qianhuiColor;
        [cell.contentView addSubview:lineLabel1];
        
    }
    
    if (array.count ==indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(60, 50-0.35, ScreenWidth-75, 0.35)];
        lineLabel1.backgroundColor=qianhuiColor;
        [cell.contentView addSubview:lineLabel1];
        
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell =(UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    if ([cell.textLabel.text isEqualToString:@"交易明细"]) {
        
        TradesViewController * Login=[[TradesViewController alloc]init];
        Login.memberId=self.memberId;
        [self.navigationController pushViewController:Login animated:YES];
        
    }
    if ([cell.textLabel.text isEqualToString:@"优惠券"]) {
        MCouponViewController * Login=[[MCouponViewController alloc]init];
           Login.memberId=self.memberId;
        [self.navigationController pushViewController:Login animated:YES];
        
        
    }
    
    if ([cell.textLabel.text isEqualToString:@"促销活动"]) {
        MemberActivityViewController * Login=[[MemberActivityViewController alloc]init];
        Login.memberId=self.memberId;
        Login.Act=@"MAct";
        [self.navigationController pushViewController:Login animated:YES];
        
        
    }
    if ([cell.textLabel.text isEqualToString:@"积分明细"]) {
        MemberfindIntegralListViewController * Login=[[MemberfindIntegralListViewController alloc]init];
        Login.memberId=self.memberId;
        [self.navigationController pushViewController:Login animated:YES];
        
        
    }
    if ([cell.textLabel.text isEqualToString:@"储值记录"]) {

        MemberCardRecordsViewController * Login=[[MemberCardRecordsViewController alloc]init];
        Login.memberId=self.memberId;
        Login.userName= [Dict objectForKey:@"userName"];
        Login.phone=[Dict objectForKey:@"phone"];
       [self.navigationController pushViewController:Login animated:YES];
        
        
    }
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  50;
}


-(void)requestData:(UIButton *)sender{
    
    TheLoginViewController* Login=[[TheLoginViewController alloc]init];
    
    [self.navigationController pushViewController:Login animated:YES];
    
    
    
}
-(void)TheMessageBtn:(UIButton *)sender{
    
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    userStatus = [user objectForKey:@"userStatus"];
    
    if ([userStatus isEqualToString:@"Y"]) {
        
        MessageViewController* Login=[[MessageViewController alloc]init];
        
        [self.navigationController pushViewController:Login animated:YES];
        
    }
    
    
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
-(void)setNavi{
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
    self.title = @"会员详情";
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     backColor,
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    
    
    
    
    
    
    UIButton*editor = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0, 30, 30)];
    [editor setImage:[UIImage imageNamed:@"huiyuan-liaotian"] forState:UIControlStateNormal];
    [editor addTarget:self action:@selector(TheMessageBtn:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:editor];
    self.navigationItem.rightBarButtonItem = myBtnBtn;
    
    
}

@end
